import React, { createContext, useContext, useState } from 'react'



const ContextDemo = createContext(null)   // 默认值为null  找不到Provider或取不到Provider值得时候用的

export default function index() {
  let [state, setState] = useState(0)
  return (
    <div>
      <ContextDemo.Provider value={{ state, setState }}>
        <Child />
      </ContextDemo.Provider>
    </div>
  )
}


const Child = () => {
  const { state, setState } = useContext(ContextDemo)
  const add = () => {
    setState(state + 1)
  }
  return (<>
    {
      state
    }
    <button onClick={add}>加一</button>
  </>)
}